package com.zygk.xmpp.xmpp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import com.zygk.library.view.LoadingDialog;
import com.zygk.xmpp.R;

public abstract class XmppLoadThread {
  private boolean isHint;
  private LoadingDialog loadingDialog;
  private Context c;

  @SuppressLint("NewApi") public XmppLoadThread(Context _mcontext) {
    isHint = true;
    c = _mcontext;

    //		FULL_TASK_EXECUTOR = (ExecutorService) Executors.newCachedThreadPool();
    new AsyncTask<Void, Integer, Object>() {

      @Override protected Object doInBackground(Void... arg0) {
        return load();
      }

      @Override protected void onPostExecute(Object result) {
        if (isHint && (loadingDialog == null || !loadingDialog.isShowing())) {
          return;
        } else {
          try {
            result(result);
            loadingDialog.dismiss();
          } catch (Throwable e) {
            e.printStackTrace();
          }
        }
      }

      @Override protected void onPreExecute() {
        if (isHint) {
          try {
            loadingDialog = new LoadingDialog(c);
            loadingDialog.show();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    }.execute();
  }

  protected abstract Object load();

  protected abstract void result(Object object);
}
